using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class UserCardCategory
	{
		public bool Exists(string CardCategory)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from MBF_UserCardCategory");
			stringBuilder.Append(" where CardCategory=@CardCategory ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CardCategory", SqlDbType.VarChar, 50)
			};
			array[0].Value = CardCategory;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public bool Add(HKB.Data.Model.UserCardCategory model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into MBF_UserCardCategory(");
			stringBuilder.Append("CardCategory,CardCategoryName,Remark)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@CardCategory,@CardCategoryName,@Remark)");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CardCategory", SqlDbType.VarChar, 50),
				new SqlParameter("@CardCategoryName", SqlDbType.NVarChar, 50),
				new SqlParameter("@Remark", SqlDbType.Text)
			};
			array[0].Value = model.CardCategory;
			array[1].Value = model.CardCategoryName;
			array[2].Value = model.Remark;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Update(HKB.Data.Model.UserCardCategory model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update MBF_UserCardCategory set ");
			stringBuilder.Append("CardCategoryName=@CardCategoryName,");
			stringBuilder.Append("Remark=@Remark");
			stringBuilder.Append(" where CardCategory=@CardCategory ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CardCategoryName", SqlDbType.NVarChar, 50),
				new SqlParameter("@Remark", SqlDbType.Text),
				new SqlParameter("@CardCategory", SqlDbType.VarChar, 50)
			};
			array[0].Value = model.CardCategoryName;
			array[1].Value = model.Remark;
			array[2].Value = model.CardCategory;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(string CardCategory)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from MBF_UserCardCategory ");
			stringBuilder.Append(" where CardCategory=@CardCategory ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CardCategory", SqlDbType.VarChar, 50)
			};
			array[0].Value = CardCategory;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool DeleteList(string CardCategorylist)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from MBF_UserCardCategory ");
			stringBuilder.Append(" where CardCategory in (" + CardCategorylist + ")  ");
			int num = Conn.ExecuteSql(stringBuilder.ToString(), new IDbDataParameter[0]);
			return num > 0;
		}
		public HKB.Data.Model.UserCardCategory GetModel(string CardCategory)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select  top 1 CardCategory,CardCategoryName,Remark from MBF_UserCardCategory ");
			stringBuilder.Append(" where CardCategory=@CardCategory ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CardCategory", SqlDbType.VarChar, 50)
			};
			array[0].Value = CardCategory;
			HKB.Data.Model.UserCardCategory userCardCategory = new HKB.Data.Model.UserCardCategory();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			HKB.Data.Model.UserCardCategory result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public HKB.Data.Model.UserCardCategory DataRowToModel(DataRow row)
		{
			HKB.Data.Model.UserCardCategory userCardCategory = new HKB.Data.Model.UserCardCategory();
			if (row != null)
			{
				if (row["CardCategory"] != null)
				{
					userCardCategory.CardCategory = row["CardCategory"].ToString();
				}
				if (row["CardCategoryName"] != null)
				{
					userCardCategory.CardCategoryName = row["CardCategoryName"].ToString();
				}
				if (row["Remark"] != null)
				{
					userCardCategory.Remark = row["Remark"].ToString();
				}
			}
			return userCardCategory;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select CardCategory,CardCategoryName,Remark ");
			stringBuilder.Append(" FROM MBF_UserCardCategory ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" CardCategory,CardCategoryName,Remark ");
			stringBuilder.Append(" FROM MBF_UserCardCategory ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + filedOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public int GetRecordCount(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) FROM MBF_UserCardCategory ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			object single = DbHelperSQL.GetSingle(stringBuilder.ToString());
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("SELECT * FROM ( ");
			stringBuilder.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				stringBuilder.Append("order by T." + orderby);
			}
			else
			{
				stringBuilder.Append("order by T.CardCategory desc");
			}
			stringBuilder.Append(")AS Row, T.*  from MBF_UserCardCategory T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				stringBuilder.Append(" WHERE " + strWhere);
			}
			stringBuilder.Append(" ) TT");
			stringBuilder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
	}
}
